package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import connection.JDCConnection;

import model.Etudiant;
import model.Facture;

public class EtudiantDAO {

	Statement stmt;
	ResultSet rs;
	
	Etudiant etudiant = new Etudiant();
	
	public boolean getValideUser(JDCConnection connection , String codeUniversel, String mPasse) {
		boolean hasRows = false;
		Statement stmt;
		ResultSet rs;
		try {
			stmt = connection.createStatement();
			System.out.println("SELECT * FROM Etudiant where codeUniversel=\""+codeUniversel+"\" and motPasse=\""+mPasse+"\"");
			rs = stmt.executeQuery("SELECT * FROM Etudiant where codeUniversel=\""+codeUniversel+"\" and motPasse=\""+mPasse+"\"");
			hasRows=rs.first();
			//	hasRows=rs.next();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return hasRows;
	}
	
	public Etudiant getEtudiantByName(JDCConnection connection , String codeUniversel) {
		
		try {

			stmt = connection.createStatement();

			rs = stmt.executeQuery("SELECT * FROM Etudiant where codeUniversel=\""+codeUniversel+"\"");
			while (rs.next()) {
				
				etudiant.setIdEtudiant(rs.getInt("idEtudiant"));
				etudiant.setNom(rs.getString("nom"));
				etudiant.setPrenom(rs.getString("prenom"));
				etudiant.setCodePermanent(rs.getString("codePermanent"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return etudiant;
	}
		
	public Etudiant getEtudiant(JDCConnection connection , int idEtudiant) {
		
		try {

			stmt = connection.createStatement();

			rs = stmt.executeQuery("SELECT * FROM Etudiant where idEtudiant="+idEtudiant);
			while (rs.next()) {
				
				
				etudiant.setIdEtudiant(rs.getInt("idEtudiant"));
				etudiant.setNom(rs.getString("nom"));
				etudiant.setPrenom(rs.getString("prenom"));
				etudiant.setCodePermanent(rs.getString("codePermanent"));
				etudiant.setCodeUniversel(rs.getString("codeUniversel"));
				etudiant.setCourriel(rs.getString("courriel"));
				etudiant.setTelephone(rs.getString("numTelephone"));
				
				etudiant.setMotDePasse(rs.getString("motPasse"));
				etudiant.setNomPrograme(rs.getString("nomProgramme"));
				etudiant.setSolde(rs.getDouble("solde"));
				etudiant.setNbCreditReussit(rs.getInt("nbCreditReussit"));
				
				
			
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return etudiant;
	}
	
	//
	public boolean saveProfile(JDCConnection connection,Etudiant etudiant) {
		Statement stmt;

		try {
			stmt = connection.createStatement();
		
			stmt.executeUpdate("Update Etudiant SET courriel = " + etudiant.getCourriel() + ", numTelephone = " + etudiant.getTelephone() +" WHERE idEtudiant = " + etudiant.getIdEtudiant());
			
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		return true;
	}
	
	
	public int setSolde(JDCConnection connection, int idEtudiant, double solde) {
		int resultat = 0;
		try {
			stmt = connection.createStatement();
			resultat = stmt.executeUpdate("UPDATE Etudiant SET solde = " + solde + " WHERE idEtudiant = " + idEtudiant);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return resultat;
	}
	
}
